﻿function ServiceAgent(defaultBaseUri) {
    this.DefaultBaseUri = defaultBaseUri;
    this.GetDataFresh = function () {
        var $body = $(document.body);
        var dataName = arguments[0];
        $body.removeData(dataName);
        return this.GetData(arguments);
    },
    this.GetData = function () {
        var $body = $(document.body);
        var dataName = arguments[0];
        
        if ($body.data(dataName) != null) return $body.data(dataName);

        var baseUri = this.DefaultBaseUri;
        if (arguments.length > 2) {
            baseUri = arguments[3];
        }
        
        var path = arguments[1];

        $.ajax({
            url: baseUri + path,
            async: false,
            success: function (data) {
                $body.data(dataName, data);
            }
        })

        return $body.data(dataName);
    }
}